package com.example.test2;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.test2.activity.BaseActivity;
import com.example.test2.broadcast.NetworkStateReceiver;
import com.example.test2.databinding.ActivityMainBinding;
import com.example.test2.event.RoleEvent;
import com.example.test2.http.Constants;
import com.example.test2.main.fragment.BuyerFragment;
import com.example.test2.main.fragment.FriendFragment;
import com.example.test2.main.fragment.MineFragment;
import com.example.test2.main.fragment.MsgFragment;
import com.example.test2.main.fragment.NewsFragment;
import com.example.test2.main.fragment.SellerFragment;
import com.example.test2.utils.Tools;
import com.example.test2.view.SendDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


// TODO: 主界面
public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private FragmentManager manager;
    private FragmentTransaction transaction;
    private SellerFragment sellerFragment;
    private BuyerFragment buyerFragment;
    private FriendFragment friendFragment;
    private NewsFragment newsFragment;
    private MsgFragment msgFragment;
    private MineFragment mineFragment;
    private NetworkStateReceiver receiver;

    @Override
    protected void initData() {
        super.initData();
        EventBus.getDefault().register(this);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        initNavigation();
        registerReceiver();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    private void initNavigation() {

        buyerFragment = new BuyerFragment();
        transaction.replace(R.id.fl_content, buyerFragment);
        transaction.commit();

        getBinding().navigation.setItemIconTintList(null);
        getBinding().navigation.setItemIconSize(60);//控制底部导航栏图标大小
        getBinding().navigation.setSelectedItemId(getBinding().navigation.getMenu().getItem(0).getItemId());

        getBinding().navigation.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    add(1);
                    break;
                case R.id.navigation_news:
                    add(2);
                    break;
                case R.id.navigation_add:
                    add(3);
                    break;
                case R.id.navigation_msg:
                    add(4);
                    break;
                case R.id.navigation_mine:
                    add(5);
                    break;
            }
            return false;
        });
    }
    private void add(int pos) {
        getBinding().navigation.getMenu().getItem(pos - 1).setChecked(true);
        remove();
        switch (pos) {
            case 1:
                if (Constants.IS_BUYER) {//买家
                    buyerFragment = new BuyerFragment();
                    transaction.replace(R.id.fl_content, buyerFragment);
                    transaction.commit();
                    Tools.setStatusBarColor(MainActivity.this, R.color.bg_title);
                } else {//卖家
                    sellerFragment = new SellerFragment();
                    transaction.replace(R.id.fl_content, sellerFragment);
                    transaction.commit();
                    Tools.setStatusBarColor(MainActivity.this, R.color.bg_seller_top);
                }
                break;
            case 2:
                if (Constants.IS_BUYER) {//买家
                    newsFragment = new NewsFragment();
                    transaction.replace(R.id.fl_content, newsFragment);
                    transaction.commit();
                    Tools.setStatusBarColor(MainActivity.this, R.color.bg_title);
                } else {//卖家
                    friendFragment = new FriendFragment();
                    transaction.replace(R.id.fl_content, friendFragment);
                    transaction.commit();
                    Tools.setStatusBarColor(MainActivity.this, R.color.bg_title);
                }
                break;
            case 3:
                new SendDialog(this).show();
                break;
            case 4:
                msgFragment = new MsgFragment();
                transaction.replace(R.id.fl_content, msgFragment);
                transaction.commit();
                Tools.setStatusBarColor(MainActivity.this, R.color.white);
                break;
            case 5:
                mineFragment = new MineFragment();
                transaction.replace(R.id.fl_content, mineFragment);
                transaction.commit();
                Tools.setStatusBarColor(MainActivity.this, R.color.bg_mine_top);
                break;
        }
    }
    private void remove() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        if (sellerFragment != null) {
            transaction.remove(sellerFragment);
        }
        if (buyerFragment != null) {
            transaction.remove(buyerFragment);
        }
        if (friendFragment != null) {
            transaction.remove(friendFragment);
        }
        if (newsFragment != null) {
            transaction.remove(newsFragment);
        }
        if (msgFragment != null) {
            transaction.remove(msgFragment);
        }
        if (mineFragment != null) {
            transaction.remove(mineFragment);
        }
    }

    @Override
    protected ActivityMainBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityMainBinding.inflate(layoutInflater);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(RoleEvent event) {
        remove();
        Constants.IS_BUYER = event.isBuyer();
        if (Constants.IS_BUYER) {//买家
            buyerFragment = new BuyerFragment();
            transaction.replace(R.id.fl_content, buyerFragment);
            transaction.commit();
            Tools.setStatusBarColor(MainActivity.this, R.color.bg_title);
            getBinding().navigation.getMenu().getItem(1).setTitle("资讯");
        } else {//卖家
            sellerFragment = new SellerFragment();
            transaction.replace(R.id.fl_content, sellerFragment);
            transaction.commit();
            Tools.setStatusBarColor(MainActivity.this, R.color.bg_seller_top);
            getBinding().navigation.getMenu().getItem(1).setTitle("淘友圈");
        }
    }

    private void registerReceiver() {
        receiver = new NetworkStateReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiver, filter);
    }

    private void unregisterReceiver() {
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        unregisterReceiver();
    }
}